package com.wang.experiment.experiment3.booksystem;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wr
 * @date 2021/10/18 20:08
 */
public class BookSystem extends JFrame {
    private final String DATA_PATH = "./book.dat";
    private BookManager manager;
    private JTable mainTable;
    private final BookSystem bookSystem = this;

    public void init() {
        initFrame();

        //header
        initHeader();

        //aside
        initAside();

        //main
        initMain();

        setVisible(true);
    }

    private void initFrame() {
        setTitle("图书管理系统");
        setSize(1080, 720);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(screenSize.width / 2 - 1080 / 2, screenSize.height / 2 - 720 / 2, 1080, 720);
        setLayout(new BorderLayout());

        manager = new BookManager();
        manager.loadData(DATA_PATH);

        //确认退出
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int result = JOptionPane.showConfirmDialog(bookSystem, "确认退出？\n所有更改将会自动保存。", "退出", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    manager.saveData(DATA_PATH);
                    System.exit(0);
                }
                super.windowClosing(e);
            }
        });
    }

    private void initHeader() {
        JLabel label = new JLabel("图书管理系统", JLabel.CENTER);
        label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 30));
        add(label, BorderLayout.NORTH);
    }

    private void initMain() {
//        Book book = new Book("java程序设计", "辛运帏、饶一梅",
//                "清华大学出版社", "978-7-302-46899-8",
//                "2001年9月", 365, "本书从Java语言的基本特点入手,全面介绍了Java语言的基本概念和编程方法,并深入介绍了Java语言的高级特性。内容涉及Java语言的基本语法、数据类型、类、异常、界面设计、小应用程序、I/O数据流、线程及网络功能等，基本覆盖了Java语言的大部分技术，是进一步使用Java语言进行技术开发的基础。");
//        manager.addBook(book);
        Object[][] tableDate = manager.getBookArray(manager.getBookList());
        mainTable = new JTable(tableDate, NAMES) {
            @Override
            public boolean isCellEditable(int row, int column) {
                //点击摘要弹出详情框
                if (column == 7) {
//                    StringBuffer summary = new StringBuffer((String) this.getValueAt(row, column));
//                    for (int i = 20; i < summary.length(); i += 20) {
//                        summary.insert(i,'\n');
//                    }
//                    JOptionPane.showMessageDialog(this, summary.toString(), "摘要", JOptionPane.INFORMATION_MESSAGE);
                    //信息框不可复制,采取Dialog实现
                    new SummaryDialog(bookSystem,(String) this.getValueAt(row,column));
                    return false;
                }
                return super.isCellEditable(row, column);
            }
        };
        //为表格设置一个只读编辑器,使其可以复制无法修改.
        JTextField tf = new JTextField();
        tf.setEditable(false);
        DefaultCellEditor editor = new DefaultCellEditor(tf);
        mainTable.setDefaultEditor(Object.class, editor);
        add(new JScrollPane(mainTable), BorderLayout.CENTER);
    }

    private void initAside() {
        JPanel aside = new JPanel(new FlowLayout());
        aside.setPreferredSize(new Dimension(120, 720));
        add(aside, BorderLayout.WEST);
        JButton showBooks = new JButton("所有图书");
        JButton addBook = new JButton("添加图书");
        JButton searchBook = new JButton("查找图书");
        JButton deleteBook = new JButton("删除图书");
        aside.add(showBooks);
        aside.add(addBook);
        aside.add(searchBook);
        aside.add(deleteBook);

        //buttonListener
        showBooks.addActionListener((e) -> showBookData(manager.getBookList()));
        addBook.addActionListener((e) -> new AddBookDialog(bookSystem, manager));
        searchBook.addActionListener(e -> new SearchBookDialog(bookSystem, manager));
        deleteBook.addActionListener(e -> new DeleteBookDialog(bookSystem, manager));

    }

    private static final String[] NAMES = {"id", "书名", "作者", "出版社", "版号", "出版日期", "总页数", "摘要"};

    public void showBookData(Book book) {
        ArrayList<Book> list = new ArrayList<>(1);
        if (book != null) {
            list.add(book);
        }
        showBookData(list);
    }

    public void showBookData(List<Book> books) {
        Object[][] bookArray = manager.getBookArray(books);
        mainTable.setModel(new DefaultTableModel(bookArray, NAMES));
    }

    public static void main(String[] args) {
        BookSystem bookSystem = new BookSystem();
        bookSystem.init();
    }
}
